package com.cqie.hospitalescort.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class JsonUtil {


    public static SerializeConfig serializeConfig;

    static {
        serializeConfig = new SerializeConfig();
        serializeConfig.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
        serializeConfig.put(java.security.Timestamp.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
        serializeConfig.put(java.sql.Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd"));
    }

    /**
     * @JSONField(format = "yyyy-MM-dd HH:mm:ss")//返回结果
     * @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")//获取参数
     */

    /**
     * 默认的处理时间 - fastjson
     *
     * @param jsonText
     * @return
     */
    public static String toJSON(Object jsonText) {
        // 在serializerFeatureList中添加转换规则
        List<SerializerFeature> serializerFeatureList = new ArrayList<SerializerFeature>();
        serializerFeatureList.add(SerializerFeature.WriteNullListAsEmpty);
        serializerFeatureList.add(SerializerFeature.DisableCircularReferenceDetect);
        serializerFeatureList.add(SerializerFeature.WriteEnumUsingToString);
        serializerFeatureList.add(SerializerFeature.WriteDateUseDateFormat);
        SerializerFeature[] serializerFeatures = serializerFeatureList
                .toArray(new SerializerFeature[serializerFeatureList.size()]);
        // 自定义序列化规则
        return JSON.toJSONString(jsonText, serializeConfig, serializerFeatures);
    }


}